package cc.ruih.meizanchen;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 帮助页面
 * @author 郭瑞坡
 *
 */
public class Help extends Activity{  
      
    GestureDetector detector;  
    ImageView chapter;	// 页面底部文章图片
    ImageView help;		// 帮助图片
    ImageButton left;		// 左
    ImageButton right;	// 右
    Boolean flag;		// 左是ture，右是false
    int page=1;
    
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.help);  
        ImageView headLogo = (ImageView) findViewById(R.id.headLogo);
		headLogo.setVisibility(View.GONE);
		ImageView setImage = (ImageView) findViewById(R.id.setting);
		setImage.setVisibility(View.GONE);
		TextView tv=(TextView) findViewById(R.id.headTitle);
		tv.setText("帮助");
        chapter = (ImageView) findViewById(R.id.chapter);
        help = (ImageView) findViewById(R.id.pic);
        left = (ImageButton) findViewById(R.id.left);
        right = (ImageButton) findViewById(R.id.right);
        left.setVisibility(View.INVISIBLE);		//设置向左的图标为不可见
        
        //将GestureDetector绑定到LinearLayout
        detector=new GestureDetector(new GestureListener());  
        LinearLayout layout=(LinearLayout)findViewById(R.id.help);  
        layout.setOnTouchListener(new TouhListener());  
        layout.setLongClickable(true);  
        
        left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPic(false, page);
			}
		});
        right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPic(true, page);
			}
		});
    }  
    
    // 翻页显示方法
    private void showPic(Boolean f, int p){
    	if (f==false) {
    		if(page<=1){
            	page=1;
            	Toast.makeText(getApplicationContext(), "这是第一页", Toast.LENGTH_SHORT).show();
            	return;
            }
            page--;
            switch (page) {
			case 1:
				left.setVisibility(View.INVISIBLE);
				help.setImageResource(R.drawable.pic1);
            	chapter.setImageResource(R.drawable.chapter1);
				break;
			case 2:
				help.setImageResource(R.drawable.pic2);
            	chapter.setImageResource(R.drawable.chapter2);
				break;
			case 3:
				help.setImageResource(R.drawable.pic3);
            	chapter.setImageResource(R.drawable.chapter3);
				break;
			case 4:
				right.setVisibility(View.VISIBLE);
				help.setImageResource(R.drawable.pic4);
            	chapter.setImageResource(R.drawable.chapter4);
				break;
			case 5:
				help.setImageResource(R.drawable.pic5);
            	chapter.setImageResource(R.drawable.chapter5);
				break;
			}
		}else if(f==true){
			if(page>=5){
            	page=5;
            	Toast.makeText(getApplicationContext(), "这已经是最后一页", Toast.LENGTH_SHORT).show();
            	return;
            }
            page++;
            switch (page) {
			case 1:
				help.setImageResource(R.drawable.pic1);
            	chapter.setImageResource(R.drawable.chapter1);
				break;
			case 2:
				left.setVisibility(View.VISIBLE);
				help.setImageResource(R.drawable.pic2);
            	chapter.setImageResource(R.drawable.chapter2);
				break;
			case 3:
				help.setImageResource(R.drawable.pic3);
            	chapter.setImageResource(R.drawable.chapter3);
				break;
			case 4:
				help.setImageResource(R.drawable.pic4);
            	chapter.setImageResource(R.drawable.chapter4);
				break;
			case 5:
				right.setVisibility(View.INVISIBLE);
				help.setImageResource(R.drawable.pic5);
            	chapter.setImageResource(R.drawable.chapter5);
				break;
			}
		}
    }
    //触摸屏幕监听  
    class TouhListener implements OnTouchListener{  
        @Override  
        public boolean onTouch(View v, MotionEvent event) {  
            return detector.onTouchEvent(event);  
        }  
    }  
    //手势滑动监听  
    class GestureListener implements OnGestureListener{  
  
        @Override  
        public boolean onDown(MotionEvent e) {  
        /*  
         * 当触摸屏幕是使用这个函数  
         */  
            return false;  
        }  
        @Override  
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                float velocityY) {  
            // e1 触摸的起始位置，e2 触摸的结束位置，velocityX X轴每一秒移动的像素速度（大概这个意思） velocityY 就是Ｙ咯  
            //手势左,上为正 ——，右，下为负正  
            if (e2.getX()-e1.getX()>50) {  // 往右滑
                //为什么是50？ 这个根据你的模拟器大小来定，看看模拟器宽度，e2.getX()-e1.getX()<屏幕宽度就ＯＫ 
				showPic(false, page);
                //要触发什么事件都在这里写就OK   
            } else if (e1.getX()-e2.getX()>50) {  	// 往左滑
				showPic(true, page);
            }  
            return false;  
        }  
  
        @Override  
        public void onLongPress(MotionEvent e) {  
          /*  
           * 长按事件  一切长事件按屏幕想要触发的事件都在这里写  
           */  
        }  
        @Override  
        public boolean onScroll(MotionEvent e1, MotionEvent e2,  
                float distanceX, float distanceY) {  
            return false;  
        }  
        @Override  
        public void onShowPress(MotionEvent e) {  
             
        }  
        @Override  
        public boolean onSingleTapUp(MotionEvent e) {  
            return false;  
        }  
    }  
}